import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-08-22
 * Time: 15:30
 */
public class MyStack {
    private  int[] elem;
    private int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }

    @Override
    public String toString() {
        return "MyStack{" +
                "elem=" + Arrays.toString(elem) +
                ", usedSize=" + usedSize +
                '}';
    }

    //压栈
    public void push(int val){
        if(isFull()){
            elem=Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++]=val;

    }
    public boolean isFull(){
        return this.usedSize==elem.length;
    }

    //出栈
    public int pop(){
        if(empty()){
            throw new StackEmptyException("栈内元素为空");
        }
        return elem[--usedSize];
    }
    public  boolean empty(){
        return usedSize==0;
    }

    //获取栈顶元素
    public int peek(){
        if(empty()){
            throw new StackEmptyException("栈内元素为空");
        }
        return elem[usedSize-1];
    }


}
